package com.moive.news.tqnewsapplication.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.moive.news.tqnewsapplication.R;
import com.moive.news.tqnewsapplication.base.Adapter.BaseListAdapter;
import com.moive.news.tqnewsapplication.entity.SearchInfo;
import com.moive.news.tqnewsapplication.networks.BaseApi;
import com.moive.news.tqnewsapplication.okmanager.OkManagerCallback;
import com.moive.news.tqnewsapplication.ui.activity.LoginActivity;
import com.moive.news.tqnewsapplication.ui.activity.PersonDetilsActivity;
import com.moive.news.tqnewsapplication.ui.fragment.FenLeiFragment;
import com.moive.news.tqnewsapplication.utils.LogUtil;
import com.moive.news.tqnewsapplication.utils.PreferenceUtil;
import com.moive.news.tqnewsapplication.utils.ToastUtils;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;
import okhttp3.Call;

/**
 * Created by Administrator on 2017/12/26.
 */
public class SearchAdapter extends BaseListAdapter<SearchInfo.DataBean> {
    private Context context;
    private int type = 0;

    public SearchAdapter(Context context) {
        this.context = context;
    }

    @Override
    protected View getItemView(int position, View convertView, ViewGroup parent, List<SearchInfo.DataBean> item) {
        ViewHeolder viewHeolder = null;
        if (viewHeolder == null) {
            viewHeolder = new ViewHeolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.adapter_fenlei02, null);
            viewHeolder.fenlei02_img = convertView.findViewById(R.id.fenlei02_img);
            viewHeolder.fenlei02_name = convertView.findViewById(R.id.fenlei02_name);
            viewHeolder.fenlei02_time = convertView.findViewById(R.id.fenlei02_time);
            viewHeolder.fenlei02_guanzhu_number = convertView.findViewById(R.id.fenlei02_guanzhu_number);
            viewHeolder.fenlei02_guanzhu = convertView.findViewById(R.id.fenlei02_guanzhu);
            convertView.setTag(viewHeolder);
        } else {
            viewHeolder = (ViewHeolder) convertView.getTag();
        }
        Glide.with(context)
                .load(item.get(position).getHeadimg())
                .centerCrop()
                .dontAnimate()
                .placeholder(R.drawable.icon)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(viewHeolder.fenlei02_img);
        viewHeolder.fenlei02_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, PersonDetilsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("id", item.get(position).getId());
                bundle.putString("user_img", item.get(position).getHeadimg());
                bundle.putString("user_name", item.get(position).getNickname());
                bundle.putBoolean("isguanzhu",item.get(position).isGuanzhu());
                intent.putExtras(bundle);
                context.startActivity(intent);
            }
        });
        viewHeolder.fenlei02_name.setText(item.get(position).getNickname());
        if (TextUtils.isEmpty(item.get(position).getSignature())) {
            viewHeolder.fenlei02_time.setText("TA不想介绍自己！");
        }
        if (item.get(position).getIsCurrentUserFollow() == 1) {
            viewHeolder.fenlei02_guanzhu.setBackgroundResource(R.drawable.fenlei02_guanzhu02);
        } else {
            viewHeolder.fenlei02_guanzhu.setBackgroundResource(R.drawable.fenlei02_guanzhu01);
        }
        viewHeolder.fenlei02_time.setText(item.get(position).getTelephone());
        final ViewHeolder finalViewHolder1 = viewHeolder;

        viewHeolder.fenlei02_guanzhu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (TextUtils.isEmpty(PreferenceUtil.getString("token",""))){
                    ToastUtils.ShortToast("你还未登录！");
                    context.startActivity(new Intent(context, LoginActivity.class));

                }if (item.get(position).getId().equals(PreferenceUtil.getString("id",""))){
                    ToastUtils.ShortToast("你不能关注自己！");
                    return;
                }
                else {
                    if (item.get(position).isGuanzhu()==false) {
                        gguanzhu(finalViewHolder1, item.get(position).getId(),position);
                    } else {
                        delteguanzhu(finalViewHolder1, item.get(position).getId(),position);
                    }
                }
            }
        });
        return convertView;
    }
    class ViewHeolder {
        CircleImageView fenlei02_img;
        TextView fenlei02_name, fenlei02_time, fenlei02_guanzhu_number;
        ImageView fenlei02_guanzhu;
    }

    //加关注
    private void gguanzhu(final ViewHeolder viewHolder, String followID,int postion) {
        OkHttpUtils.post()
                .url(BaseApi.GUANZHU_ONE)
                .addParams("token", PreferenceUtil.getString("token", ""))
                .addParams("followID", followID)
                .build().execute(new OkManagerCallback(context) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                LogUtil.e("lidaming", e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                LogUtil.i("lidaming", response.toString());
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.optInt("code") == 0) {
                        viewHolder.fenlei02_guanzhu.setBackgroundResource(R.drawable.fenlei02_guanzhu02);
               arrayLists.get(postion).setGuanzhu(true);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    //取消关注
    private void delteguanzhu(final ViewHeolder viewHolder, String followID,int postion) {
        OkHttpUtils.post()
                .url(BaseApi.UN_GUANZHU)
                .addParams("token", PreferenceUtil.getString("token", ""))
                .addParams("followID", followID)
                .build().execute(new OkManagerCallback(context) {
            @Override
            public void onError(Call call, Exception e, int id) {
                super.onError(call, e, id);
                LogUtil.e("lidaming", e.toString());
            }

            @Override
            public void onResponse(String response, int id) {
                super.onResponse(response, id);
                LogUtil.i("lidaming", response.toString());
                try {
                    JSONObject jsonObject = new JSONObject(response);
                    if (jsonObject.optInt("code") == 0) {
                        viewHolder.fenlei02_guanzhu.setBackgroundResource(R.drawable.fenlei02_guanzhu01);
                        arrayLists.get(postion).setGuanzhu(false);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }

}
